/**
 * Subclass of {@link LibFunction} which implements the call-cc in lua
 */
package org.luaj.vm2.lib;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaThread;
import org.luaj.vm2.LuaValue;

/**
 * @author Marie-Diana
 *
 */
public class CallccLib extends TwoArgFunction {
	Globals globals;

	@Override
	public LuaValue call(LuaValue modname, LuaValue env) {
		globals = env.checkglobals();
		LuaTable callcc = new LuaTable();
		callcc.set("callcc", new callcc());
		env.set("callcc", callcc);
		env.get("package").get("loaded").set("callcc", callcc);
		return callcc;	
	}

	final class callcc extends LibFunction {
		public LuaValue call(LuaValue f) {
			//Call CoroutineLib ?
			return new LuaThread(globals, f.checkfunction());
		}
	}

}
